const experss = require('express')
const router = experss.Router()
const mysql = require('mysql')
let db = mysql.createPool({
    host: 'localhost',
    port: 3306,
    database: 'userdb',
    user: 'root',
    password: 'root'
})




router.get('/user', (req, res) => {
    db.query('SELECT * FROM `user`', (err, data) => {
        res.send(data)
    })
})

//修改
router.post('/upDate', (req, res) => {
        // console.log(req.query);
        // console.log(req.query.id);
        db.query('UPDATE `user` SET ? WHERE id=?', [req.body, req.body.id], (err, data) => {
            if (!err) {
                res.send('修改成功');
            } else {
                res.send('修改失败')
            }
        })
    })
    //添加
router.post('/add', (req, res) => {
    db.query('INSERT INTO `user` SET ?', req.body, (err, data) => {
        if (!err) {
            res.send('插入成功')
        } else {
            res.send('插入失败')
        }
    })
})

//删除
router.post('/del', (req, res) => {
    db.query('DELETE FROM `user` WHERE id=?', req.body.id, function(err, data) {
        if (data.affectedRows == 1) {
            res.send('删除成功')
        } else {
            res.send('删除失败')
        }
    })
})






module.exports = router